using System.Collections.Generic;
using System.Threading.Tasks;

namespace Matrix.Notification.Notifications
{
    /// <summary>
    /// 通知模板数据传输对象
    /// </summary>
    public class NotificationTemplateDto
    {
        public Guid Id { get; set; }
        public string Name { get; set; } = string.Empty;
        public string Title { get; set; } = string.Empty;
        public string Content { get; set; } = string.Empty;
        public string Type { get; set; } = string.Empty;
        public bool IsEnabled { get; set; } = true;
        public Dictionary<string, object>? Metadata { get; set; }
    }

    /// <summary>
    /// 模板渲染服务接口
    /// </summary>
    public interface ITemplateRenderingService
    {
        /// <summary>
        /// 渲染模板
        /// </summary>
        /// <param name="template">通知模板</param>
        /// <param name="data">模板数据</param>
        /// <returns>渲染结果</returns>
        Task<TemplateRenderResult> RenderAsync(NotificationTemplateDto template, Dictionary<string, object>? data = null);

        /// <summary>
        /// 渲染模板内容
        /// </summary>
        /// <param name="templateContent">模板内容</param>
        /// <param name="data">模板数据</param>
        /// <returns>渲染结果</returns>
        Task<TemplateRenderResult> RenderContentAsync(string templateContent, Dictionary<string, object>? data = null);

        /// <summary>
        /// 预览模板
        /// </summary>
        /// <param name="template">通知模板</param>
        /// <param name="sampleData">示例数据</param>
        /// <returns>预览结果</returns>
        Task<TemplateRenderResult> PreviewAsync(NotificationTemplateDto template, Dictionary<string, object>? sampleData = null);

        /// <summary>
        /// 验证模板语法
        /// </summary>
        /// <param name="templateContent">模板内容</param>
        /// <returns>验证结果</returns>
        Task<TemplateValidationResult> ValidateTemplateAsync(string templateContent);

        /// <summary>
        /// 获取模板变量列表
        /// </summary>
        /// <param name="templateContent">模板内容</param>
        /// <returns>模板变量列表</returns>
        Task<List<string>> GetTemplateVariablesAsync(string templateContent);

        /// <summary>
        /// 批量渲染模板
        /// </summary>
        /// <param name="templates">模板列表</param>
        /// <param name="dataList">数据列表</param>
        /// <returns>渲染结果列表</returns>
        Task<List<TemplateRenderResult>> RenderBatchAsync(
            List<(NotificationTemplateDto template, Dictionary<string, object>? data)> templates);
    }

    /// <summary>
    /// 模板渲染结果
    /// </summary>
    public class TemplateRenderResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 渲染后的标题
        /// </summary>
        public string Title { get; set; } = string.Empty;

        /// <summary>
        /// 渲染后的内容
        /// </summary>
        public string Content { get; set; } = string.Empty;

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; } = string.Empty;

        /// <summary>
        /// 使用的变量列表
        /// </summary>
        public List<string> UsedVariables { get; set; } = new();

        /// <summary>
        /// 缺失的变量列表
        /// </summary>
        public List<string> MissingVariables { get; set; } = new();

        /// <summary>
        /// 渲染耗时（毫秒）
        /// </summary>
        public long RenderTimeMs { get; set; }

        /// <summary>
        /// 附加信息
        /// </summary>
        public Dictionary<string, object>? AdditionalData { get; set; }
    }

    /// <summary>
    /// 模板验证结果
    /// </summary>
    public class TemplateValidationResult
    {
        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid { get; set; }

        /// <summary>
        /// 错误消息列表
        /// </summary>
        public List<string> Errors { get; set; } = new();

        /// <summary>
        /// 警告消息列表
        /// </summary>
        public List<string> Warnings { get; set; } = new();

        /// <summary>
        /// 发现的变量列表
        /// </summary>
        public List<string> Variables { get; set; } = new();

        /// <summary>
        /// 建议信息
        /// </summary>
        public List<string> Suggestions { get; set; } = new();

        /// <summary>
        /// 验证耗时（毫秒）
        /// </summary>
        public long ValidationTimeMs { get; set; }
    }
}